package com.yytek.am.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.yytek.am.constants.AkkaConstants;
import com.yytek.am.constants.MessageType;
import com.yytek.am.core.rule.EventRuleGenerate;
import com.yytek.common.client.net.IotMqttClient;
import com.yytek.common.core.utils.DateUtils;
import com.yytek.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.am.mapper.FmRuleGenerateMapper;
import com.yytek.am.domain.FmRuleGenerate;
import com.yytek.am.service.IFmRuleGenerateService;

import javax.annotation.Resource;

/**
 * 告警生成规则定义Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-10-10
 */
@Service
public class FmRuleGenerateServiceImpl implements IFmRuleGenerateService 
{
    @Autowired
    private FmRuleGenerateMapper fmRuleGenerateMapper;
    @Resource
    IotMqttClient iotMqttClient;
    /**
     * 查询告警生成规则定义
     * 
     * @param id 告警生成规则定义主键
     * @return 告警生成规则定义
     */
    @Override
    public FmRuleGenerate selectFmRuleGenerateById(Long id)
    {
        return fmRuleGenerateMapper.selectFmRuleGenerateById(id);
    }

    /**
     * 查询告警生成规则定义列表
     * 
     * @param fmRuleGenerate 告警生成规则定义
     * @return 告警生成规则定义
     */
    @Override
    public List<FmRuleGenerate> selectFmRuleGenerateList(FmRuleGenerate fmRuleGenerate)
    {
        return fmRuleGenerateMapper.selectFmRuleGenerateList(fmRuleGenerate);
    }

    /**
     * 新增告警生成规则定义
     * 
     * @param fmRuleGenerate 告警生成规则定义
     * @return 结果
     */
    @Override
    public int insertFmRuleGenerate(FmRuleGenerate fmRuleGenerate)
    {
        String userId = SecurityUtils.getUserId().toString();
        fmRuleGenerate.setOperator(userId);
        fmRuleGenerate.setCreateTime(DateUtils.getNowDate());
        Integer num = fmRuleGenerateMapper.insertFmRuleGenerate(fmRuleGenerate);
        if(num != null && num > 0) {
            //发送mqtt，akka接收变更
            sendAkkaMqttRuleGenerateChange(AkkaConstants.EventRuleUpdateType.UPDATE, fmRuleGenerate);
        }
        return num;
    }

    /**
     * 发送mqtt，akka接收变更告警生成
     * @param add
     * @param fmRuleGenerate
     */
    public void sendAkkaMqttRuleGenerateChange(int type, FmRuleGenerate fmRuleGenerate) {
        EventRuleGenerate eventRuleGenerate = new EventRuleGenerate();
        eventRuleGenerate.setId(fmRuleGenerate.getId());
        eventRuleGenerate.setCreateTime(fmRuleGenerate.getCreateTime().getTime());
        eventRuleGenerate.setUpdateTime(fmRuleGenerate.getUpdateTime().getTime());
        eventRuleGenerate.setAlarmDefineId(fmRuleGenerate.getAlarmDefineId());
        eventRuleGenerate.setAccCount(fmRuleGenerate.getAccCount().intValue());
        eventRuleGenerate.setAccTime(fmRuleGenerate.getAccTime().intValue());
        eventRuleGenerate.setCalculateExpress(fmRuleGenerate.getCalculateExpress());
        String json = JSON.toJSONString(fmRuleGenerate);
        iotMqttClient.publish(MessageType.EventRule.EVENT_RULE_GENERATE.replace("+",
                String.valueOf(type)),json);
    }

    /**
     * 修改告警生成规则定义
     * 
     * @param fmRuleGenerate 告警生成规则定义
     * @return 结果
     */
    @Override
    public int updateFmRuleGenerate(FmRuleGenerate fmRuleGenerate)
    {
        String userId = SecurityUtils.getUserId().toString();
        fmRuleGenerate.setOperator(userId);
        fmRuleGenerate.setUpdateTime(DateUtils.getNowDate());
        Integer num = fmRuleGenerateMapper.updateFmRuleGenerate(fmRuleGenerate);
        //发送mqtt，akka接收变更
        sendAkkaMqttRuleGenerateChange(AkkaConstants.EventRuleUpdateType.UPDATE,fmRuleGenerate);
        return num;
    }

    /**
     * 批量删除告警生成规则定义
     * 
     * @param ids 需要删除的告警生成规则定义主键
     * @return 结果
     */
    @Override
    public int deleteFmRuleGenerateByIds(Long[] ids)
    {
        if(ids.length > 0){
            for (int i = 0; i < ids.length; i++) {
                long id = ids[i];
                //发送mqtt，akka接收变更
                FmRuleGenerate fmRuleGenerate = selectFmRuleGenerateById(id);
                sendAkkaMqttRuleGenerateChange(AkkaConstants.EventRuleUpdateType.DELETE,fmRuleGenerate);
            }
        }
        return fmRuleGenerateMapper.deleteFmRuleGenerateByIds(ids);
    }

    /**
     * 删除告警生成规则定义信息
     * 
     * @param id 告警生成规则定义主键
     * @return 结果
     */
    @Override
    public int deleteFmRuleGenerateById(Long id)
    {
        //发送mqtt，akka接收变更
        FmRuleGenerate fmRuleGenerate = selectFmRuleGenerateById(id);
        sendAkkaMqttRuleGenerateChange(AkkaConstants.EventRuleUpdateType.DELETE,fmRuleGenerate);
        Integer num = fmRuleGenerateMapper.deleteFmRuleGenerateById(id);
        return num;
    }

    @Override
    public boolean execitByAlarmDefineId(Long alarmDefineId) {
        FmRuleGenerate cx = new FmRuleGenerate();
        cx.setAlarmDefineId(alarmDefineId);
        List<FmRuleGenerate> list = selectFmRuleGenerateList(cx);
        if(CollectionUtil.isNotEmpty(list)){
            return true;
        }else {
            return false;
        }
    }
}
